package com.google.code.mochaccino.framework.hibernate.provider.configuration.helper;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Properties;
import org.hibernate.cfg.Environment;
import org.hibernate.context.internal.JTASessionContext;
import org.hibernate.context.internal.ManagedSessionContext;
import org.hibernate.context.internal.ThreadLocalSessionContext;
import org.hibernate.context.spi.CurrentSessionContext;
import org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory;
import org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory;
import org.hibernate.engine.transaction.spi.TransactionFactory;
import org.hibernate.service.jta.platform.internal.BitronixJtaPlatform;
import org.hibernate.service.jta.platform.spi.JtaPlatform;

/** Configuration the Transaction Management Strategy for hibernate */
public final class ConfigurationHelperTransaction implements ConfigurationHelper {
	/** Session Context Class Management (Default to JTA) */
	private Class<? extends CurrentSessionContext> currentSessionContextClass = null;

	/** Transaction Strategy (Default to JTA) */
	private Class<? extends TransactionFactory> transactionStrategy = null;

	/** Transaction Manager Lookup (Default to Bitronix Transaction Manager) */
	private Class<? extends JtaPlatform> jtaPlatform = null;

	/** Constructor */
	public ConfigurationHelperTransaction( Class<? extends CurrentSessionContext> currentSessionContextClass, Class<? extends TransactionFactory> transactionStrategy, Class<? extends JtaPlatform> jtaPlatform ) {
		this.currentSessionContextClass = currentSessionContextClass;
		this.transactionStrategy = transactionStrategy;
		this.jtaPlatform = jtaPlatform;
	}

	/** Bitronix Transaction Manager */
	public static ConfigurationHelperTransaction bitronix() {
		return jta( BitronixJtaPlatform.class );
	}

	/** JTA */
	public static ConfigurationHelperTransaction jta( Class<? extends JtaPlatform> lookupStrategy ) {
		return new ConfigurationHelperTransaction( JTASessionContext.class, JtaTransactionFactory.class, lookupStrategy );
	}

	/** Local/JDBC */
	public static ConfigurationHelperTransaction local() {
		return new ConfigurationHelperTransaction( ThreadLocalSessionContext.class, JdbcTransactionFactory.class, null );
	}

	/** Local/JDBC */
	public static ConfigurationHelperTransaction managed() {
		return new ConfigurationHelperTransaction( ManagedSessionContext.class, JdbcTransactionFactory.class, null );
	}

	@Override
	public void configure( Properties p ) {
		p.put( Environment.CURRENT_SESSION_CONTEXT_CLASS, currentSessionContextClass.getName() );
		p.put( Environment.TRANSACTION_STRATEGY, transactionStrategy.getName() );
		if ( jtaPlatform != null ) {
			p.put( Environment.JTA_PLATFORM, jtaPlatform.getName() );
		}
	}

	public void setCurrentSessionContextClass( Class<? extends CurrentSessionContext> currentSessionContextClass ) {
		this.currentSessionContextClass = currentSessionContextClass;
	}

	public void setJtaPlatform( Class<? extends JtaPlatform> jtaPlatform ) {
		this.jtaPlatform = jtaPlatform;
	}

	public void setTransactionStrategy( Class<? extends TransactionFactory> transactionStrategy ) {
		this.transactionStrategy = transactionStrategy;
	}
}
